package gameObject.item;

import gameObject.Unit;
import gameObject.effect.BulletEffect;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

import base.ItemFrame;
import main.Res;

public abstract class ItemArm extends ItemEquipment {
	protected ItemFrame[] frameSequence;
	protected int currentFrame;
	private boolean isActived;
	protected Sprite hand;

	protected int targetX = 0;
	protected int targetY = 0;
	protected int idleTime = 0;
	
	final protected int delayTime;
	final protected int range;
	final protected int damage;
	

	public int getTargetX() {
		return targetX;
	}

	public int getTargetY() {
		return targetY;
	}
	protected ItemArm(int id,String name,int cost,int dam,int range,int delay) {
		super(id,name,cost);
		this.damage = dam;
		this.range = range;
		this.delayTime = delay;
		currentFrame = 0;
		isActived = false;
	}

	public void setOwner(Unit owner) {
		hand = Res.createHandSprite(owner.getID());
		super.setOwner(owner);

	}
	public int getRange(){
		return range;
	}
	public void attack(int x,int y) {
		if(isActived){
			return;
		}
		targetX = x;
		targetY = y;
		if(idleTime>delayTime){
			isActived = true;
		}
	}

	public boolean isAir(){
		return false;
	}
	public void update() {
		if (isActived()) {
			if (currentFrame == frameSequence.length - 1) {
				currentFrame = 0;
				idleTime = 0;
				isActived = false;
			} else {
				currentFrame++;
				updatePosition();
			}
		}
		else{
			idleTime ++;
		}
	}

	public BulletEffect getCurrentEffect() {
		if (isActived() && frameSequence[currentFrame].isActive)
		{
			return createEffect();
		}
		return null;
	}

	public BulletEffect createEffect() {
		return null;
	}
	public int getItemType(){
		return ARM;
	}

	public void updatePosition() {
		ItemFrame f = frameSequence[currentFrame];
		double a = Math.toRadians(owner.getRotation());
		double sin = Math.sin(a);
		double cos = Math.cos(a);
		double dx = f.x*cos - f.y*sin;
		double dy = f.x*sin + f.y*cos;
		setPosition(owner.getX() + (int)dx, owner.getY() + (int)dy);
		setRotation(owner.getRotation() + f.rotation);
		sprite.setFrame(f.frame);
	}

	public void setPosition(int x, int y) {
		sprite.setRefPixelPosition(x, y);
		hand.setRefPixelPosition(x, y);
	}

	public void addTo(LayerManager layer) {
		layer.insert(sprite, 2);
		layer.insert(hand, 2);

	}

	public void removeFrom(LayerManager layer) {
		layer.remove(hand);
		super.removeFrom(layer);

	}
	
	public void paint(Graphics g){
		sprite.paint(g);
		hand.paint(g);
	}
	public String getInfoText() {
		String info = name + "\n$(0)" + cost + "\n";
		info += "$(6)" + damage + " ";
		info += "$(7)" + range + " ";
		info += "$(8)" + delayTime + " ";
		return info;
	}

	public boolean isActived() {
		return isActived;
	}
	
	public int getDamage(){
		return (int) (owner.getAttack()*(double)damage/100);
	}
}
